package intant.enums.test;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class CodeEnumTestCase {

    @Test
    public void testGetByCode() {
        // 通过code获取枚举实例，查找不到返回null
        Assertions.assertEquals(CodeEnum.getByCode("1", SampleCodeEnum.class), SampleCodeEnum.YES);
        Assertions.assertNull(CodeEnum.getByCode("2", SampleCodeEnum.class));
    }

    @Test
    public void testCodeEquals() {
        Assertions.assertTrue(SampleCodeEnum.YES.codeEquals("1"));
    }

    @Test
    public void testCodeEqualsIgnoreType() {
        Assertions.assertTrue(SampleCodeEnum.YES.codeEqualsIgnoreType(1));
        Assertions.assertTrue(SampleCodeEnum.YES.codeEqualsIgnoreType("1"));
    }

    @Test
    public void testGetByCodeDef() {
        // 通过code获取枚举实例，设置默认值，如果查找不到返回默认值
        Assertions.assertEquals(CodeEnum.getByCode("1", SampleCodeEnum.class, SampleCodeEnum.NO), SampleCodeEnum.YES);
        Assertions.assertEquals(CodeEnum.getByCode("2", SampleCodeEnum.class, SampleCodeEnum.NO), SampleCodeEnum.NO);
    }

    @Test
    public void testContains() {
        // 查看枚举中是否存在指定 code 编码的实例
        Assertions.assertTrue(CodeEnum.contains("0", SampleCodeEnum.class));
        Assertions.assertFalse(CodeEnum.contains("2", SampleCodeEnum.class));
    }

    enum SampleCodeEnum implements CodeEnum<String> {
        YES("1"),
        NO("0");

        private final String code;

        SampleCodeEnum(String code) {
            this.code = code;
        }

        @Override
        public String getCode() {
            return code;
        }

    }

}
